<?php

namespace app\admin\controller\events;

use app\common\controller\Backend;
use fast\Http;
use think\Controller;
use think\Request;
use think\Log;


/**
 * 游戏活动控制
 *
 * @icon fa fa-circle-o
 */
class Eventsys extends Backend
{
    
    /**
     * VEventsys模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('VEventsys');
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("typeList", $this->model->getTypeList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
     * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
        /**
     * 重写添加在添加中，加入post，到服务器
     */
    
        /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            if ($params)
            {
                foreach ($params as $k => &$v)
                {
                    $v = is_array($v) ? implode(',', $v) : $v;
                }
                try
                {
                    //是否采用模型验证
                    if ($this->modelValidate)
                    {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
                        $this->model->validate($validate);
                    }
                    if($params['params'] == 0){
                        $params['params'] = '{}';
                    }
                    if($params['type'] == 1 && ($params['params'] != 0)){
                        $params['params'] = json_encode(['kindId'=>$params['params']]);
                    }
                    if($params['type'] == 2 && ($params['params'] != 0)){
                        $params['params'] = json_encode(['activityId'=>$params['params']]);
                    }

                    $result = $this->model->save($params);
                    if ($result !== false)
                    {
                         //获取到数据，进行把数据post发送到服务器
						$params['startTime'] = strtotime($params['startTime']);
						$params['endTime'] = strtotime($params['endTime']);
                        $params['params'] = $params['params'];
						Http::get($this->GameDBConfig['api_url'].'Eventsys',$params);
                        $this->success();
                    }
                    else
                    {
                        $this->error($this->model->getError());
                    }
                }
                catch (\think\exception\PDOException $e)
                {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }
	  /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $row = $this->model->get($ids);
        if (!$row)
            $this->error(__('No Results were found'));
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            if ($params)
            {
                foreach ($params as $k => &$v)
                {
                    $v = is_array($v) ? implode(',', $v) : $v;
                }
                try
                {
                    //是否采用模型验证
                    if ($this->modelValidate)
                    {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
                        $row->validate($validate);
                    }
                    if($params['params'] == 0){
                        $params['params'] = '{}';
                    }
                    if($params['type'] == 1 && ($params['params'] != 0)){
                        $params['params'] = json_encode(['kindId'=>$params['params']]);
                    }
                    if($params['type'] == 2 && ($params['params'] != 0)){
                        $params['params'] = json_encode(['activityId'=>$params['params']]);
                    }

                    $result = $row->save($params);
                    if ($result !== false)
                    {
                         //获取到数据，进行把数据post发送到服务器
                        //发生变化通知服务器
                         //获取到数据，进行把数据post发送到服务器
						$params['startTime'] = strtotime($params['startTime']);
						$params['endTime'] = strtotime($params['endTime']);
                        $params['params'] = $params['params'];
						Http::get($this->GameDBConfig['api_url'].'Eventsys',$params);
                        $this->success();
                    }
                    else
                    {
                        $this->error($row->getError());
                    }
                }
                catch (think\exception\PDOException $e)
                {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $row['params'] = json_decode($row['params'],true);
        if($row['type'] == 1){
            $row['params'] = isset($row['params']['kindId'])?$row['params']['kindId']:0;
        }
        if($row['type'] == 2){
            $row['params'] = isset($row['params']['activityId'])?$row['params']['kindId']:0;
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
	
	     /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids)
        {
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds))
            {
                $count = $this->model->where($this->dataLimitField, 'in', $adminIds)->delete($ids);
            }
            else
            {
                $count = $this->model->destroy($ids);
            }
            if ($count)
            {
                $data = [
                    'activityId' => $ids,
                    'del' => 1,
                ];
				Log::debug($data,'wwwwwwwwwwww');
				Http::get($this->GameDBConfig['api_url'].'Eventsys',$data);
                $this->success();
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }

}
